package com.unytech.wisdomcommunity.activity;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.unytech.wisdomcommunity.R;
import com.unytech.wisdomcommunity.constant.NetConstant;
import com.unytech.wisdomcommunity.pager.ComplainRepairPager;
import com.viewpagerindicator.TabPageIndicator;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * "投诉报修记录"页面 ComlainRepairDetails
 */
public class ComlainRepairRecordActivity extends Activity implements View.OnClickListener {

    private TabPageIndicator mTabPageIndicator;
    private String[] title = {"投诉记录","报修记录"};
    private OkHttpClient mClient;

    private Call mCall;
    private ImageView mBackIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complain_repair_record);

        inintView();
    }

    private void inintView() {
        mBackIv = ((ImageView) findViewById(R.id.comlainrepair_record_back_iv));
        mBackIv.setOnClickListener(this);

        ViewPager viewPager = (ViewPager) findViewById(R.id.comlainrepair_record_vp);
        mTabPageIndicator = (TabPageIndicator)findViewById(R.id.comlainrepair_record_tpi);

        viewPager.setAdapter(mPageAdapter);
        mTabPageIndicator.setViewPager(viewPager);

        mTabPageIndicator.setOnPageChangeListener(TabOnPageChangeListener);

        initData();
    }
    //给Tpi设置监听
    private ViewPager.OnPageChangeListener TabOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    private void initData() {
        //在这里   发送联网请求
        mClient = new OkHttpClient();
        // 设置get请求参数的方式
        Uri uri = Uri.parse(NetConstant.BASE_URL).buildUpon()
                .appendPath("msds/ws/getComplainRepair")   // 拼接路径, 路径开头不要有斜杠
                .appendQueryParameter("pageNo", "1")
                .appendQueryParameter("pageSize", "15")
                .build();

        Log.i("Uri是", "uri------" + uri.toString());

        // 1. 封装请求
        final Request request = new Request.Builder()
                //.url(BASE_URL + "/msds/ws/addComplainRepair?pageNo=1&pageSize=15")   // url后面直接拼接参数
                .url(uri.toString())
                //.addHeader("userid", "")   // 加请求头
                .get()
                .build();

        // 2. 发送请求
        mCall = mClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure", "异常信息: "+e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                int code = response.code();
                Log.d("onResponse: ", " 响应码"+ code );
                String data = response.body().string();

                mPageAdapter.notifyDataSetChanged();
                //mTabPageIndicator.notifyDataSetChanged();
            }
        });
    }

    private PagerAdapter mPageAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return title ==null?0:title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ComplainRepairPager complainRepairPager = new ComplainRepairPager(container.getContext(),mClient,mCall);
            container.addView(complainRepairPager.getView());
            //View view = complainRepairPager.getView();
            //Log.d(TAG, "instantiateItem: "+view);

            return complainRepairPager.getView();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.comlainrepair_record_back_iv:
                finish();
                break;
            default:
                break;
        }
    }
}